package com.change.study.data.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.change.study.data.core.Finder;
import com.change.study.data.core.Pagination;
import com.change.study.data.core.Updater;
import com.change.study.data.dao.ArticleCommentDao;
import com.change.study.data.dto.ArticlePageRpc;
import com.change.study.data.entity.ArticleComment;
import com.change.study.data.service.ArticleCommentService;

@Service
@Transactional
public class ArticleCommentServiceImpl implements ArticleCommentService {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public ArticleComment findById(Long id) {
		ArticleComment entity = dao.findById(id);
		return entity;
	}

    @Transactional
	public ArticleComment save(ArticleComment bean) {
		dao.save(bean);
		return bean;
	}

    @Transactional
	public ArticleComment update(ArticleComment bean) {
		Updater<ArticleComment> updater = new Updater<ArticleComment>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

    @Transactional
	public ArticleComment deleteById(Long id) {
		ArticleComment bean = dao.deleteById(id);
		return bean;
	}

    @Transactional	
	public ArticleComment[] deleteByIds(Long[] ids) {
		ArticleComment[] beans = new ArticleComment[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private ArticleCommentDao dao;

	@Autowired
	public void setDao(ArticleCommentDao dao) {
		this.dao = dao;
	}

	
	@Transactional(readOnly = true)
	@Override
	public ArticlePageRpc pageByArticleId(long id, int pageNo, int pageSize) {
		ArticlePageRpc result=null;
		
		Finder finder=Finder.create();
		finder.append("from ArticleComment a where a.articleid=:articleid");
		finder.setParam("articleid", id);
		finder.append("  order by a.id desc");
		Pagination p=	dao.find(finder, pageNo, pageSize);
		result=new ArticlePageRpc(p);
		return result;
	}
}